package Visao;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import Controle.AlunoControle;
import Controle.AlunoSIControle;
import Controle.ControleFuncionario;
import Modelo.Aluno;
import Modelo.AlunoSI;
import Modelo.Funcionario;
import javax.swing.JFormattedTextField;
import javax.swing.ImageIcon;

public class Inicio extends JFrame {

	private JPanel contentPane;
	private JFormattedTextField textFieldCPF;
	private JTextField textFieldNome;
	private JTextField textFieldMatricula;
	private JButton btnNewButton;
	private JMenuBar menuBar;
	private JMenu mnInicio;
	private JMenu mnCadastro;
	private JMenu mnBuscas;
	private AlunoControle controleAluno;
	private Aluno aluno;
	private ControleDeTelas telas;
	private AlunoSI alunoSi;
	private AlunoSIControle controleAlunoSi;
	private ControleFuncionario controleFuncionario;
	private Funcionario funcionario;
	private JButton btnDeletar;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public Inicio() throws ParseException {
		controleFuncionario = new ControleFuncionario();
		funcionario = new Funcionario();
		alunoSi = new AlunoSI();
		controleAlunoSi = new AlunoSIControle();
		telas = new ControleDeTelas();
		aluno = new Aluno();
		controleAluno = new AlunoControle();
		setTitle("Support Watchful");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 451, 300);
		setLocationRelativeTo(null);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnInicio = new JMenu("Inicio");
		mnInicio.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
			}
		});
		menuBar.add(mnInicio);
		
		mnCadastro = new JMenu("Cadastro Aluno");
		mnCadastro.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
				telas.getCadastros().setVisible(true);
				dispose();
			}
		});
		menuBar.add(mnCadastro);
		
		mnBuscas = new JMenu("Buscas");
		mnBuscas.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				telas.getBusca().setVisible(true);
				dispose();
			}
		});
		
		JMenu mnCadastroFuncionario = new JMenu("Cadastro Funcionario");
		mnCadastroFuncionario.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				telas.getCadastroFuncionario().setVisible(true);
				dispose();
			}
		});
		menuBar.add(mnCadastroFuncionario);
		menuBar.add(mnBuscas);
		
		JMenu menu = new JMenu("");
		menuBar.add(menu);
		
		JMenu mnVisitante = new JMenu("Visitante");
		mnVisitante.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				try {
					telas.getVisitante().setVisible(true);
					dispose();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		menuBar.add(mnVisitante);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		MaskFormatter msk = new MaskFormatter("***.***.***-**");
		
		JPanel panel = new JPanel();
		panel.setForeground(new Color(204, 51, 51));
		panel.setBackground(new Color(0, 139, 139, 50));
		panel.setBounds(10, 9, 197, 165);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblBuscarPessoa = new JLabel("Buscar Pessoa");
		lblBuscarPessoa.setBounds(10, 9, 105, 20);
		panel.add(lblBuscarPessoa);
		lblBuscarPessoa.setForeground(new Color(0, 0, 0));
		lblBuscarPessoa.setBackground(new Color(0, 0, 0));
		lblBuscarPessoa.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 15));
		
		JLabel lblCpf = new JLabel("CPF");
		lblCpf.setBounds(10, 67, 36, 14);
		panel.add(lblCpf);
		lblCpf.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCpf.setForeground(new Color(0, 0, 0));
		
		textFieldCPF = new JFormattedTextField(msk);
		textFieldCPF.setBounds(49, 65, 136, 20);
		panel.add(textFieldCPF);
		textFieldCPF.setColumns(10);
		
		btnNewButton = new JButton("OK\r\n");
		btnNewButton.setBounds(119, 109, 56, 23);
		panel.add(btnNewButton);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if(controleAluno.buscarPorCpf(textFieldCPF.getText()) != null){
					aluno = controleAluno.buscarPorCpf(textFieldCPF.getText());
					funcionario = null;
					}else{
					funcionario = controleFuncionario.buscarPorCpf(textFieldCPF.getText());
						aluno = null;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(aluno == null){
					
				}else{
				telas.getPresenca().carregarAluno(aluno);
				telas.getPresenca().setVisible(true);
				dispose();
				}
				if(funcionario == null){
					
				}else{
					telas.getPresenca().carregarFuncionario(funcionario);
					telas.getPresenca().setVisible(true);
					dispose();
				}
				
				
			}
		});
		
		JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setForeground(new Color(204, 51, 51));
		panel_2.setBackground(new Color(0, 139, 139, 50));
		panel_2.setBounds(10, 179, 197, 62);
		contentPane.add(panel_2);
		
		JLabel lblParaDeletarAlgun = new JLabel("Para deletar Algum dado");
		lblParaDeletarAlgun.setBounds(10, 0, 180, 16);
		panel_2.add(lblParaDeletarAlgun);
		lblParaDeletarAlgun.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblParaDeletarAlgun.setForeground(new Color(0, 0, 0));
		
		btnDeletar = new JButton("Deletar");
		btnDeletar.setBounds(10, 25, 91, 23);
		panel_2.add(btnDeletar);
		btnDeletar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					telas.getExcluir().setVisible(true);
					dispose();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		JLabel lblSemIdentificao = new JLabel("Sem Identifica\u00E7\u00E3o");
		lblSemIdentificao.setBounds(267, 36, 134, 14);
		contentPane.add(lblSemIdentificao);
		lblSemIdentificao.setForeground(new Color(0, 0, 0));
		lblSemIdentificao.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 15));
		
		JPanel panel_1 = new JPanel();
		panel_1.setForeground(new Color(204, 51, 51));
		panel_1.setBackground(new Color(0, 139, 139, 50));
		panel_1.setBounds(217, 11, 217, 163);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblNome = new JLabel("Nome");
		lblNome.setBounds(10, 70, 46, 14);
		panel_1.add(lblNome);
		lblNome.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblNome.setForeground(new Color(0, 0, 0));
		
		textFieldNome = new JTextField();
		textFieldNome.setBounds(97, 68, 98, 20);
		panel_1.add(textFieldNome);
		textFieldNome.setColumns(10);
		
		textFieldMatricula = new JTextField();
		textFieldMatricula.setBounds(97, 98, 98, 20);
		panel_1.add(textFieldMatricula);
		textFieldMatricula.setColumns(10);
		
		JLabel lblNMatricula = new JLabel("Curso/Cargo\r\n\r\n");
		lblNMatricula.setBounds(10, 99, 88, 17);
		panel_1.add(lblNMatricula);
		lblNMatricula.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblNMatricula.setForeground(new Color(0, 0, 0));
		
		JButton btnOk = new JButton("OK");
		btnOk.setBounds(130, 129, 65, 23);
		panel_1.add(btnOk);
		
		
		JLabel lblBuscarPessoaSem = new JLabel("Salvar Pessoa");
		lblBuscarPessoaSem.setBounds(63, 11, 121, 14);
		panel_1.add(lblBuscarPessoaSem);
		lblBuscarPessoaSem.setForeground(new Color(0, 0, 0));
		lblBuscarPessoaSem.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 15));
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon("H:\\workspace\\SupportWatchful\\Imagens\\i=magem lois.jpg"));
		lblNewLabel.setBounds(0, 0, 443, 252);
		contentPane.add(lblNewLabel);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				alunoSi.setCurso(textFieldMatricula.getText());
				alunoSi.setNome(textFieldNome.getText());
				alunoSi.setData(new Date());
				
				try {
					controleAlunoSi.salvar(alunoSi);
					JOptionPane.showMessageDialog(null, "Pessoa registrada!");
					textFieldNome.setText("");
					textFieldMatricula.setText("");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}
